package cn.iocoder.yudao.module.resource.controller.admin.story;

import cn.iocoder.yudao.module.infra.service.file.FileService;
import cn.iocoder.yudao.module.infra.service.file.UploadFileResult;
import org.springframework.web.bind.annotation.*;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import jakarta.validation.constraints.*;
import jakarta.validation.*;
import jakarta.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.resource.controller.admin.story.vo.*;
import cn.iocoder.yudao.module.resource.dal.dataobject.story.StoryDO;
import cn.iocoder.yudao.module.resource.service.story.StoryService;

@Tag(name = "管理后台 - 场景")
@RestController
@RequestMapping("/lm/story")
@Validated
public class StoryController {

    @Resource
    private StoryService storyService;
    @Resource
    private FileService fileService;

    @PostMapping("/create")
    @Operation(summary = "创建场景")
    @PreAuthorize("@ss.hasPermission('lm:story:create')")
    public CommonResult<Integer> createStory(@Valid @RequestBody StorySaveReqVO createReqVO) {
        return success(storyService.createStory(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新场景")
    @PreAuthorize("@ss.hasPermission('lm:story:update')")
    public CommonResult<Boolean> updateStory(@Valid @RequestBody StorySaveReqVO updateReqVO) {
        storyService.updateStory(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除场景")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('lm:story:delete')")
    public CommonResult<Boolean> deleteStory(@RequestParam("id") Integer id) {
        storyService.deleteStory(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得场景")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('lm:story:query')")
    public CommonResult<StoryRespVO> getStory(@RequestParam("id") Integer id) {
        StoryDO story = storyService.getStory(id);
        StoryRespVO respVO = BeanUtils.toBean(story, StoryRespVO.class);
        UploadFileResult file = fileService.getFile(story.getFileId(), null);
        respVO.setFileUrl(file.getFileUrl());
        return success(respVO);
    }

    @GetMapping("/page")
    @Operation(summary = "获得场景分页")
    @PreAuthorize("@ss.hasPermission('lm:story:query')")
    public CommonResult<PageResult<StoryRespVO>> getStoryPage(@Valid StoryPageReqVO pageReqVO) {
        PageResult<StoryDO> pageResult = storyService.getStoryPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, StoryRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出场景 Excel")
    @PreAuthorize("@ss.hasPermission('lm:story:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportStoryExcel(@Valid StoryPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<StoryDO> list = storyService.getStoryPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "场景.xls", "数据", StoryRespVO.class,
                        BeanUtils.toBean(list, StoryRespVO.class));
    }

}